﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BacteriaSimulation.Simulation
{
    class CannibalBacteriumStrategy : BacteriumSimulationStrategy
    {
        protected Vector2D NewSpeed = null;

        public override void PerformSimulationStep(Bacterium bacterium)
        {
            bacterium.UnGrow(bacterium.EnergyIncrement);

            if (bacterium.Energy <= 0)
            {
                bacterium.State = BacteriumState.DEAD;
                return;
            }

            if (NewSpeed != null)
            {
                bacterium.Speed = NewSpeed;
                NewSpeed = null;
            }
        }

        public override void WallCollisionDetected(Bacterium bacterium, WALL_COLLISION_TYPE collisionType)
        {
            switch (collisionType)
            {
                case WALL_COLLISION_TYPE.BOTTOM:
                    {
                        bacterium.Speed.YComponent *= -1;
                        break;
                    }
                case WALL_COLLISION_TYPE.RIGHT:
                    {
                        bacterium.Speed.XComponent *= -1;
                        break;
                    }
                case WALL_COLLISION_TYPE.TOP:
                    {
                        bacterium.Speed.YComponent *= -1;
                        break;
                    }
                case WALL_COLLISION_TYPE.LEFT:
                    {
                        bacterium.Speed.XComponent *= -1;
                        break;
                    }
            }
        }

        public override void BacteriumCollisionDetected(Bacterium bacterium, Bacterium collidedBacterium)
        {
            if (bacterium.State != BacteriumState.DEAD
                && collidedBacterium.Strategy is CyanobacteriumSimulationStrategy 
                && collidedBacterium.State != BacteriumState.DEAD
                && collidedBacterium.Energy < bacterium.Energy)
            {
                collidedBacterium.State = BacteriumState.DEAD;
                if (bacterium.Energy < (bacterium.MaxEnergy * 10.05))
                {
                    bacterium.Grow(collidedBacterium.Energy);
                }
            }
        }
    }
}
